package chatNetwork;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JList;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class MultiChatClient extends JFrame implements Runnable {
	ServerSocket server = null;
	Socket sock = null;


	private JPanel contentPane;
	private JButton btnEnd;
	private JButton btnStart;
	private JScrollPane scrollPane_1;
	private JComboBox comboBox;
	private JTextField msgIn;
	private JButton btnSend;
	private JLabel lblNewLabel_1;
	private JTextPane result;
	private JScrollPane scrollPane;
	private JList list;
	private JLabel lblNewLabel;
	private JTextField serverIP;
	private JTextField userID;
	private JButton btnWp;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MultiChatClient frame = new MultiChatClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MultiChatClient() {
		setTitle("MultiChat Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 750, 416);
		contentPane = new JPanel();
		contentPane.setBackground(Color.DARK_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getBtnEnd());
		contentPane.add(getBtnStart());
		contentPane.add(getScrollPane_1());
		contentPane.add(getComboBox());
		contentPane.add(getMsgIn());
		contentPane.add(getBtnSend());
		contentPane.add(getScrollPane_2());
		contentPane.add(getTextField_1());
		contentPane.add(getTextField_2());
		contentPane.add(getBtnWp());
	}

	@Override
	public void run() {	

	}
	public void sendMsg(){

	}
	public void dataSet(String a){

	}

	public void serverConnect(){		
		try{
//			Thread t = new Thread(this);
//			t.start();			
			sock = new Socket(serverIP.getText(),4444);
						
			ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
			
			SocketData sd = new SocketData("login", userID.getText(), "방가방가~ :)", null);
			oos.writeObject(sd);
			
			oos.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		


	}

	public void serverDisconnect(){		

	}



	private JButton getBtnEnd() {
		if (btnEnd == null) {
			btnEnd = new JButton("Server End");
			btnEnd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverDisconnect();
				}
			});
			btnEnd.setBackground(Color.DARK_GRAY);
			btnEnd.setBounds(612, 10, 110, 23);
		}
		return btnEnd;
	}
	private JButton getBtnStart() {
		if (btnStart == null) {
			btnStart = new JButton("Server Start");
			btnStart.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverConnect();
				}
			});
			btnStart.setBackground(Color.DARK_GRAY);
			btnStart.setBounds(490, 10, 110, 23);
		}
		return btnStart;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(235, 43, 487, 275);
			scrollPane_1.setColumnHeaderView(getLblNewLabel_1());
			scrollPane_1.setViewportView(getResult());
		}
		return scrollPane_1;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setBackground(Color.DARK_GRAY);
			comboBox.setBounds(12, 328, 103, 23);
		}
		return comboBox;
	}
	private JTextField getMsgIn() {
		if (msgIn == null) {
			msgIn = new JTextField();
			msgIn.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					int a = e.getKeyCode();
					if(a == e.VK_ENTER){
						sendMsg();
					}

				}
			});
			msgIn.setBackground(Color.DARK_GRAY);
			msgIn.setText("Message");
			msgIn.setColumns(10);
			msgIn.setBounds(127, 329, 487, 22);
		}
		return msgIn;
	}
	private JButton getBtnSend() {
		if (btnSend == null) {
			btnSend = new JButton("Send");
			btnSend.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					sendMsg();
				}
			});
			btnSend.setBackground(Color.DARK_GRAY);
			btnSend.setBounds(625, 328, 97, 23);
		}
		return btnSend;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Chat Room");
			lblNewLabel_1.setOpaque(true);
			lblNewLabel_1.setBackground(Color.PINK);
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel_1;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setBackground(Color.PINK);
		}
		return result;
	}
	private JScrollPane getScrollPane_2() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 43, 211, 242);
			scrollPane.setViewportView(getList());
			scrollPane.setColumnHeaderView(getLblNewLabel());
		}
		return scrollPane;
	}
	private JList getList() {
		if (list == null) {
			list = new JList();
			list.setBackground(Color.PINK);
		}
		return list;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("User List");
			lblNewLabel.setOpaque(true);
			lblNewLabel.setBackground(Color.PINK);
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel;
	}
	private JTextField getTextField_1() {
		if (serverIP == null) {
			serverIP = new JTextField();
			serverIP.setBackground(Color.DARK_GRAY);
			serverIP.setText("Server IP Address");
			serverIP.setBounds(12, 11, 338, 22);
			serverIP.setColumns(10);
		}
		return serverIP;
	}
	private JTextField getTextField_2() {
		if (userID == null) {
			userID = new JTextField();
			userID.setBackground(Color.DARK_GRAY);
			userID.setText("ID");
			userID.setBounds(362, 11, 116, 21);
			userID.setColumns(10);
		}
		return userID;
	}
	private JButton getBtnWp() {
		if (btnWp == null) {
			btnWp = new JButton("Finish Whisper");
			btnWp.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			btnWp.setForeground(Color.BLACK);
			btnWp.setBackground(Color.DARK_GRAY);
			btnWp.setBounds(12, 295, 211, 23);
		}
		return btnWp;
	}
}
